home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 836 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.4 KB

  1. From: Philippe Verdy <100105.3120@compuserve.com>
  2. Message-ID: <4j4n7j$hjh@dub-news-svc-4.compuserve.com>
  3. X-Original-Date: 24 Mar 1996 23:52:19 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 25 Mar 96 04:26:48 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: DWP clarification...
  9. Organization: CompuServe Incorporated
  10. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  11.     iQBFAgUBMVYgveEDnX0m9pzZAQHpXgGAgpbFJkBgJHPK6no0jAI8fnYCG6q+fU0C
  12.     o0VklYJv79LRP2F/NyngPIsMHgkWGOfg
  13.     =vK69
  14.  
  15. Paul Russell aka Rusty <Rusty.Russell@adelaide.maptek.com.au> s'icrit :
  16. > The April DWP, 9.2 paragraph 12:
  17. > 12A  static  data  member,  enumerator, member of an anonymous union, or
  18. >   nested type shall not have the same name as its class.
  19. > Does this mean immediately nested?  Or nested at all? ie. Is this code
  20. > legal?
  21. > struct A
  22. > {
  23. >   struct B
  24. >   {
  25. >     struct A {};
  26. >   };
  27. > };
  28. > Also, is there a similar restriction on namespaces?  I couldn't find it
  29. > anywhere...
  30.  
  31. I think not, because the nested class A is not a member of the
  32. enclosing class A. It will not referenced inside the
  33. enclosing class A directly because it has to explicitly refer
  34. class B to access one of its members (the nested class A would
  35. be explicitly referenced by B::A, provided the nested class
  36. B::A is PUBLICly defined in B, else it would not be accessible
  37. by ::A, which cannot inherit from ::A::B::A ).
  38.  
  39. However the problem resides in the class B implementation.
  40. What if class B references 'A' ?
  41. I think that the local nested A definition is prioritary
  42. chosen, like in the following case :
  43.   class A;
  44.  
  45.   class B
  46.   {
  47.     B() {
  48.        A item; // item is of type ::B::A, not ::A
  49.     }
  50.     class A
  51.     {
  52.       ...
  53.     };
  54.   };
  55. In that case the nested class A is a member of B, so any
  56. implementation of B which refers to A refer to ::B::A and
  57. not to ::A. This is to make the class B definition independent
  58. of its context (whever or not there was an outside A
  59. declaration).
  60. Each class constitutes a namespace by its own, and names are
  61. always searched in that namespace before searching any
  62. outside contexts.
  63.  
  64. So in your case, if a method or initializer of a member of B
  65. references "A", "B::A" will always be referenced at first.
  66. You can override this by using "::A" instead of simply "A"
  67. which refers to "B::A"...
  68.  
  69. Now what if class B is declared as a friend of the enclosing
  70. class ::A ? E.g. :
  71. class A {
  72.   class B {
  73.   private:
  74.     class A { // this is a private member of B
  75.       friend class ::A; // but now ::A can access it
  76.     };
  77.   };
  78. };
  79. This simply overrides the protected or private
  80. access restriction to public, but it does not change the
  81. namespace extent, so that the same rule applies.
  82.  
  83. If class ::A::B::A is defined as a private or protected member
  84. of ::A::B, and the ::A::B::A definition does not include class
  85. ::A as a friend, there is no access possible from ::A to
  86. ::A::B::A which remains unaccessible.
  87. So if ::A implementation uses "A", it means ::A (itself),
  88. but there will be no way to refer to the private ::A::B::A,
  89. unless you add the previous friend declaration.
  90.  
  91. Is that clear ?
  92. ---
  93. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  94. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  95. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  96. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  97. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  98.